/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.web.elmt;

import java.util.*;

import org.sng.util.slog.SLog;
import org.sng.util.var.SString;
import org.sng.util.var.SVoid;

public class NodeTag extends Node {
	
	public NodeTag()
	{
		super();
	}
	
	public NodeTag(int n)
	{
		super(n);
	}
	
	public NodeTag(String id)
	{
		this.PushProperty("id", id);
	}
	
	public NodeTag(String tag, String id)
	{
		this.PushProperty("tag", tag);
		this.PushProperty("id", id);
	}
	
	public NodeTag Create()
	{
		return new NodeTag();
	}
	
	public NodeTag Create(int n)
	{
		return new NodeTag(n);
	}
	
	public NodeTag Create(String id, RuntimeEnvironment re)
	{
		NodeTag o = new NodeTag();
		
		o.m_properties.put("id", new SString(id));
		
		return o;
	}
	
	public String GetType()
	{
		return "write";
	}
	
	public StringBuffer Execute(RuntimeEnvironment re)
	{
		StringBuffer result = new StringBuffer();
		
		try {
			//result.append(this.Do(re));
			
			//this.AppendPostList(re);
			
			// Output beginning tag
			SVoid prop = null;
			String tag;
			if ((prop = this.GetProperty("tag")) != null) {
				tag = prop.GetString(re);
				this.RemoveProperty("tag");
			}
			else {
				tag = "blank";
			}
			
			result.append("<x:");
			result.append(tag);
			
			for (Iterator<Map.Entry<String, SVoid>> iter = this.m_properties.entrySet().iterator(); iter.hasNext();){
				Map.Entry<String, SVoid> element = iter.next();
                String key = element.getKey();
                SVoid value = element.getValue();
                
                result.append(" ");
                result.append(key);
                result.append("=");
                
                String s = value.toString();
                if (SString.IsEmpty(s)) {
                	result.append("\"\"");
                }
                else if (s.charAt(0) == '$') {
                	result.append(s.substring(1, s.length()));
				}
                else {
					result.append("\"");
					result.append(s);
					result.append("\"");
				}
            }
			
			result.append(">");
			
			// Output child
			ListIterator<Node> interator = this.m_children.listIterator();
			
	        while (interator.hasNext())
	        {
	        	result.append(interator.next().Execute(re));
	        }
	        
	        // Output end tag
	        result.append("</x:");
			result.append(tag);
			result.append(">");
		} catch (Exception e) {
			SLog.println("Webpage runtime error on line " + this.m_currLineNumber);
			e.printStackTrace();
		}
		
		return result;
	}

}
